/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

/**
 * 
 */
package edu.ucla.cs.typecast.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import edu.ucla.cs.typecast.bloomfilter.BloomFilter;
import edu.ucla.cs.typecast.scope.NetScope;

/**
 *
 * @date Jul 27, 2007
 */
public abstract class AbstractUDPTransportSender implements TransportSender {

	private EndPoint source; 
	private EndPoint dest; 
	private DatagramSocket socket; 
	private NetScope scope; 
	private EndPointListener localEndPointListener; 
	
	public AbstractUDPTransportSender(EndPoint source, EndPoint target) throws IOException { 
		this.dest = target; 
		socket = createSocket(source, target); 
		this.source = new EndPoint(socket.getLocalAddress(), socket.getLocalPort()); 
	}
	/* (non-Javadoc)
	 * @see edu.ucla.cs.typecast.net.TransportSender#close()
	 */
	public void close() throws IOException {
		if (localEndPointListener != null) { 
			try { 
				localEndPointListener.deactivate(source); 
			} catch(Throwable t) {}
		}

		try { 
			shutdownSocket(socket); 
		} catch(Throwable t) {}
	}
		

	/* (non-Javadoc)
	 * @see edu.ucla.cs.typecast.net.TransportSender#send(edu.ucla.cs.typecast.bloomfilter.BloomFilter, byte[], int, int)
	 */
	public void send(BloomFilter targetBloomFilter, byte[] buf, int offset,
			int length) throws IOException {
		byte[] transportBuf = new byte[BloomFilter.SIZE + length]; 
		System.arraycopy(targetBloomFilter.getValue(), 0, transportBuf, 0, BloomFilter.SIZE); 
		System.arraycopy(buf, 0, transportBuf, BloomFilter.SIZE, length); 
		DatagramPacket packet = new DatagramPacket(transportBuf, 0, transportBuf.length, dest.getAddress(), dest.getPort()); 
		socket.send(packet); 
	}

	public void setNetScope(NetScope scope) throws IOException { 		
		this.scope = scope; 
	}
	
	public NetScope getNetScope() { 
		return scope; 
	}

	protected DatagramSocket getSocket() { 
		return this.socket; 
	}
	
	protected void setSocket(DatagramSocket socket) { 
		this.socket = socket; 
	}

	public synchronized EndPointListener getLocalEndPointListener() { 
		if (localEndPointListener == null) { 
			localEndPointListener = new StaticUDPEndPointListener(socket); 
		}
		return this.localEndPointListener; 
	}
	
	abstract protected DatagramSocket createSocket(EndPoint source, EndPoint target) throws IOException; 
	abstract protected void shutdownSocket(DatagramSocket socket) throws IOException; 
}
